<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link href="/component/pear/css/pear.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/layer-css.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">时间</label>
                    <div class="layui-inline" id="test6">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="startDate" id="startDate" class="layui-input"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="endDate" id="endDate" class="layui-input"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="borrowing-table" lay-filter="borrowing-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">

    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="empty">
        <i class="layui-icon layui-icon-delete"></i>
        清空
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;

        // 设置最大可选的日期
        function maxDate() {
            var now = new Date();
            return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
        }

        //日期范围
        laydate.render({
            elem: '#test6',
            max: maxDate(),
            //设置开始日期、日期日期的 input 选择器
            //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
            range: ['#startDate', '#endDate'],
            theme: '#000000'
        });

        let cols = [
            [
                {title: '载体名称', field: 'cabinetName', align: 'center'},
                {title: '档案名称', field: 'file_name', align: 'center'},
                {title: '用户名', field: 'user_name', align: 'center'},
                {title: '部门名称', field: 'deptName', align: 'center'},
                {title: '备注', field: 'remarks', align: 'center'},
                {
                    title: '操作模块', field: 'module_type', align: 'center', templet: function (d) {
                        if (d.module_type === 1) {
                            return '<span>登录模块</span>';
                        } else if (d.module_type === 2) {
                            return '<span>档案记录</span>';
                        } else if (d.module_type === 3) {
                            return '<span>报警记录</span>';
                        } else if (d.module_type === 4) {
                            return '<span>分类记录</span>';
                        }

                    }
                },
                {
                    title: '档案状态', field: 'data_status', align: 'center', templet: function (d) {
                        if (d.data_status === 0) {
                            return '<span>借阅</span>';
                        } else if (d.data_status === 1) {
                            return '<span>归还</span>';
                        } else if (d.data_status === 2) {
                            return '<span>出库未取走</span>';
                        } else if (d.data_status === 3) {
                            return '<span>归还未入库</span>';
                        } else if (d.data_status === 4) {
                            return '<span>删除档案</span>';
                        } else if (d.data_status === 5) {
                            return '<span>新增档案</span>';
                        }
                    }
                },
                {title: '操作内容', field: 'operation', align: 'center'},
                {title: '操作时间', field: 'create_time', align: 'center', sort: true},
            ]
        ]

        tableIns = table.render({
            elem: '#borrowing-table',
            url: '/borrowing/records/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'borrowing-table',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['borrowing-table'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("borrowing-table", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })


        table.on('toolbar(borrowing-table)', function (obj) {
            if (obj.event === 'empty') {
                window.empty();
            }
        });

        form.on('submit(role-query)', function (data) {
            table.reload('borrowing-table', {where: data.field})
            return false;
        });

        window.empty = function () {
            layer.confirm('<div>确定要删除清空吗?</div>', {
                icon: 3,
                title: ['删除确认', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: '/borrowing/records/clear',
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 1800});
                            table.reload('borrowing-table');
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            });
        }
        table.on('toolbar(borrowing-table)', function (obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        // 刷新按钮
        window.refresh = function () {
            table.reload('borrowing-table');
        }
    })
</script>
</body>
</html>
